{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#some magicto show the images inside the notebook\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%pylab inline\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import cv2\n",
    "from matplotlib import pyplot as plt\n",
    "from mrf import MRFStereo, read_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initial stereo image\n",
    "\n",
    "In `Create_depth_map.ipynb` notebook, we've developed algorithm which extracts noisy stereo image from 2 images. During MRFStereo initialization, we load previously computed (by our algorithm) depth propabilities. Then we scale them to [0, 100] integers. We use markov random field and belife propagation algorithm to smooth depth image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.title('Noisy stereo image')\n",
    "_ = MRFStereo({}).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Truncated linear model as smoothness function\n",
    "We run experiments on diffrent parameters for smoothness function\n",
    "\n",
    "$$ smoothness(i,j) = \\lambda * min(abs(i-j), st) $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrf = MRFStereo(\n",
    "    dict(\n",
    "        LAMBDA=20,\n",
    "        SMOOTHNESS_TRUNC=2,\n",
    "        squared=False\n",
    "    )\n",
    ")\n",
    "_ = mrf.run(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "mrf = MRFStereo(\n",
    "    dict(\n",
    "        LAMBDA=150,\n",
    "        SMOOTHNESS_TRUNC=5,\n",
    "        squared=False\n",
    "    )\n",
    ")\n",
    "_ = mrf.run(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrf = MRFStereo(\n",
    "    dict(\n",
    "        LAMBDA=400,\n",
    "        SMOOTHNESS_TRUNC=5,\n",
    "        squared=False\n",
    "    )\n",
    ")\n",
    "_ = mrf.run(30)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Truncated quadratic model as smothness function\n",
    "$$ smoothness(i,j) = \\lambda * min((i-j)^2, st) $$\n",
    "\n",
    "Using quadratic model reduces the noise, but on the other hand blurs image sagnificantly. Linear model performs better."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mrf = MRFStereo(\n",
    "    dict(\n",
    "        LAMBDA=400,\n",
    "        SMOOTHNESS_TRUNC=14,\n",
    "        squared=True\n",
    "    )\n",
    ")\n",
    "_ = mrf.run(30)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
